package com.aionengine.gameserver.services.custom;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.aionemu.commons.utils.Rnd;
import com.aionengine.gameserver.ai2.AIState;
import com.aionengine.gameserver.ai2.AbstractAI;
import com.aionengine.gameserver.ai2.NpcAI2;
import com.aionengine.gameserver.ai2.manager.WalkManager;
import com.aionengine.gameserver.model.EmotionType;
import com.aionengine.gameserver.model.gameobjects.Npc;
import com.aionengine.gameserver.network.aion.serverpackets.SM_EMOTION;
import com.aionengine.gameserver.utils.PacketSendUtility;
import com.aionengine.gameserver.model.gameobjects.VisibleObject;
import com.aionengine.gameserver.model.templates.spawns.SpawnTemplate;
import com.aionengine.gameserver.services.FFAInstance;
import com.aionengine.gameserver.services.custom.PvPNPC.NpcType;
import com.aionengine.gameserver.spawnengine.SpawnEngine;
import com.aionengine.gameserver.spawnengine.VisibleObjectSpawner;

/**
 *
 * @author BlackCore
 */
public class CustomSPAWN {
    
    public static void spawnPortal(NpcType npcType, int npcId, int worldId, int instanceId, float x, float y, float z, byte heading) {
		SpawnTemplate spawn = SpawnEngine.addNewSpawn(worldId, npcId, x, y, z, heading, 300);
		VisibleObjectSpawner.spawnCustomNpc(npcType, spawn, instanceId);
	}
	public static void spawnPortal(NpcType teleportType, int npcId, int worldId, float x, float y, float z, byte heading) {
		spawnPortal(teleportType, npcId, worldId, 1, x, y, z, heading);
	}
        private static VisibleObject spawnCustomMob(int worldId, int instanceId, int npcId, float x, float y, float z, byte heading) {
		SpawnTemplate template = SpawnEngine.addNewSingleTimeSpawn(worldId, npcId, x, y, z, heading);
		return SpawnEngine.spawnObject(template, instanceId);
	}
        
        private static VisibleObject spawn(int worldId, int instanceId, int npcId, float x, float y, float z, byte heading, int respTime) {
		SpawnTemplate template = SpawnEngine.addNewCustomSpawn(worldId, instanceId, npcId, x, y, z, heading, respTime);
		return SpawnEngine.spawnObject(template, instanceId);
             
	}
        
        public static void spawnAll() {
                Portal();
                PVPspawn();
	}
        public static void spawnFarmMobsForPvp() {
        	spawnCustomMob(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 282120, 292f, 996f, 552f , (byte) 0);
        	spawnCustomMob(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 281032, 292f, 996f, 552f , (byte) 0);
        	spawnCustomMob(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 231073, 292f, 996f, 552f , (byte) 0);
	}
        
      //FFA Manager Spawm
        public static void spawnFarmHealForFFA() {
        	float[] rnd = HealFFA.get(Rnd.get(0, HealFFA.size() - 1));
        	spawn(FFAManager.getFFAInstanceMap().getId(), FFAManager.getFFAInstanceId(), 730770, rnd[0], rnd[1], rnd[2], (byte) 0, 3600);
        }
      //FFA Manager Spawm Random
        private static List<float[]> HealFFA = new ArrayList<float[]>(Arrays.asList(
        	new float[]{466.8695f, 628.2449f, 338.75357f},
            new float[]{374.57883f, 435.9185f, 367.875f},
            new float[]{365.09726f, 321.21088f, 380.4146f}));
        
        //FFA Manager Spawm
        public static void spawnFarmMobsForFFA() {
        	float[] rnd = FarmFFA.get(Rnd.get(0, FarmFFA.size() - 1));
        	spawnCustomMob(FFAManager.getFFAInstanceMap().getId(), FFAManager.getFFAInstanceId(), 701428, rnd[0], rnd[1], rnd[2], (byte) 0);
        }
        //FFA Manager Spawm Random
        private static List<float[]> FarmFFA = new ArrayList<float[]>(Arrays.asList(
        	new float[]{332.31494f, 274.5715f, 384.55338f},
            new float[]{460.73972f, 590.52423f, 329.02695f},
            new float[]{301.7451f, 389.14597f, 369.22943f}));
        
        public static void spawnFarmMobsForBG() {
        	runwalk((Npc) spawnCustomMob(FFAInstance.getBGInstanceMap().getId(), FFAInstance.getBGInstanceId(), 831013, 754.39185f, 1300.978f, 1183.8933f, (byte) 0), "BGEVENT_ELITE_ASMO_01");
        	runwalk((Npc) spawnCustomMob(FFAInstance.getBGInstanceMap().getId(), FFAInstance.getBGInstanceId(), 831013, 754.39185f, 1300.978f, 1183.8933f, (byte) 0), "BGEVENT_ELITE_ASMO_02");
        	runwalk((Npc) spawnCustomMob(FFAInstance.getBGInstanceMap().getId(), FFAInstance.getBGInstanceId(), 831013, 754.39185f, 1300.978f, 1183.8933f, (byte) 0), "BGEVENT_ELITE_ASMO_03");
        }
       
        private static void Portal(){
            spawnPortal(NpcType.MEDAL, 730036, 600030000, 260.6331f, 1745.747f, 295.8144f, (byte) 0);
            spawnPortal(NpcType.PVP, 730490, 600030000, 314.039f, 1736.2529f,296.18274f,(byte) 59);
            spawnPortal(NpcType.FARM, 730719, 600030000, 295f, 1723f,300f,(byte) 119);
        }
        
       private static void PVPspawn() {
		switch(PVPInstanceService.getPVPInstanceMap()) {
			case TIAMARANTA:
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 204191, 1359.028f, 1718.2596f, 22.607029f, (byte) 3, 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 203726, 1726.7404f, 1339.9542f, 39f, (byte) 60, 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 281458, 1861f, 1547f, 177f, (byte) 3, 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 281458, 1862f, 1536f, 177f , (byte) 3, 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 281458, 1862f, 1531f, 177f, (byte) 3, 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 1655.9082f, 1355.7101f, 32.69948f, (byte) 48 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 1657.0448f, 1356.3729f, 32.7968f, (byte) 48 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 1657.9824f, 1357.618f, 32.761513f, (byte) 45 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 1659.0116f, 1358.6958f, 32.731354f, (byte) 45 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 1660.1833f, 1359.4657f, 32.702618f, (byte) 45 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 1661.2382f, 1360.6074f, 32.6593f, (byte) 45 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 1662.7104f, 1361.5819f, 32.9094f, (byte) 45 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 1663.7854f, 1362.675f, 32.94925f, (byte) 45 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 1665.2346f, 1364.1487f, 32.96089f, (byte) 45 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 1666.47f, 1365.4048f, 33.42543f, (byte) 45 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 1667.3564f, 1366.3064f, 33.407467f, (byte) 45 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 1668.7747f, 1367.7484f, 33.392723f, (byte) 45 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 1670.1489f, 1368.2681f, 33.384735f, (byte) 34 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 1712.5032f, 1316.8143f, 40.83861f, (byte) 81 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 1705.7894f, 1319.5696f, 40.00502f, (byte) 83 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 1698.5892f, 1323.3141f, 38.669056f, (byte) 82 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 1718.8326f, 1327.1f, 40.01686f, (byte) 48 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 1723.7119f, 1330.6938f, 39.874706f, (byte) 44 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 1640.6686f, 1364.8484f, 28.273918f, (byte) 47 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 1655.7814f, 1379.6449f, 28.039558f, (byte) 43 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 1648.6545f, 1372.3119f, 27.762812f, (byte) 42 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 1398.2006f, 1671.0374f, 20.727423f, (byte) 108 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 1408.0558f, 1683.119f, 20.436739f, (byte) 106 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 1419.3068f, 1692.156f, 20.530466f, (byte) 107 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 1395.2239f, 1687.3389f, 20.705095f, (byte) 108 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 1396.1914f, 1688.5161f, 20.701487f, (byte) 106 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 1397.3005f, 1689.784f, 20.699205f, (byte) 106 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 1398.2137f, 1690.9686f, 20.693794f, (byte) 107 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 1399.2566f, 1692.4744f, 20.684187f, (byte) 108 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 1400.7661f, 1694.0344f, 20.684847f, (byte) 107 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 1402.0981f, 1695.713f, 20.678545f, (byte) 106 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 1403.0621f, 1696.7224f, 20.678667f, (byte) 105 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 1404.3662f, 1698.089f, 20.823362f, (byte) 105 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 1405.9171f, 1699.7771f, 20.987907f, (byte) 105 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 1407.4993f, 1701.4977f, 21.546234f, (byte) 105 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 1372.1315f, 1730.2504f, 22.377718f, (byte) 108 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 1365.9575f, 1723.238f, 22.46957f, (byte) 108 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 1404.8402f, 1746.3802f, 22.34724f, (byte) 16 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 1408.3306f, 1740.4093f, 21.960796f, (byte) 12 , 3600);
        spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 1410.7998f, 1734.7251f, 21.996157f, (byte) 10 , 3600);
				break;
			case UNDERGROUND_KATALAM:
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 203679, 306.69507f, 685.13806f, 562.3989f, (byte) 50, 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 203194, 477.68842f, 1039.8761f, 564.4654f, (byte) 10, 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 501.41907f, 1022.2964f, 564.24066f, (byte) 93 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 498.26257f, 1022.6142f, 564.40753f, (byte) 92 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 495.16312f, 1022.70575f, 564.40753f, (byte) 92 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 492.42477f, 1022.33136f, 564.40753f, (byte) 92 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 489.25266f, 1022.5712f, 564.40753f, (byte) 92 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 487.01968f, 1022.23157f, 564.40753f, (byte) 92 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 503.65616f, 1022.20557f, 564.2062f, (byte) 92 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 485.73935f, 1044.7686f, 564.6356f, (byte) 86 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209018, 499.2914f, 1046.2574f, 564.6479f, (byte) 89 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 298.5645f, 698.90826f, 562.3715f, (byte) 31 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 301.12717f, 698.5085f, 562.3669f, (byte) 31 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 303.08887f, 698.21857f, 562.36334f, (byte) 32 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 305.49884f, 698.1312f, 562.36255f, (byte) 29 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 307.99442f, 698.17004f, 562.4148f, (byte) 27 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 271.34204f, 680.78485f, 565.9927f, (byte) 61 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 271.49313f, 683.11566f, 565.9927f, (byte) 61 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 271.75134f, 686.2952f, 565.9927f, (byte) 61 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 271.44827f, 689.2326f, 565.9927f, (byte) 61 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 271.70773f, 692.2141f, 565.9927f, (byte) 62 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 271.7217f, 695.2139f, 565.9927f, (byte) 62 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 209218, 271.52237f, 698.15344f, 565.9927f, (byte) 61 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 281458, 288.94397f, 1039.8298f, 551.6948f, (byte) 91 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 281458, 283.82965f, 1040.7284f, 551.6948f, (byte) 88 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 281458, 279.71555f, 1040.9719f, 551.6948f, (byte) 88 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 281458, 275.5114f, 1041.2207f, 551.6948f, (byte) 88 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 281458, 563.5819f, 789.1008f, 552.35846f, (byte) 89 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 281458, 568.2065f, 788.87616f, 552.2758f, (byte) 89 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 281458, 567.60315f, 740.5436f, 552.25f, (byte) 88 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 281458, 563.91003f, 740.65546f, 552.2556f, (byte) 89 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 281458, 479.1294f, 513.35583f, 567.27814f, (byte) 8 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 281458, 478.43436f, 516.5256f, 565.2601f, (byte) 10 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 281458, 476.633f, 519.62585f, 564.0944f, (byte) 10 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 281458, 474.07452f, 522.9519f, 563.74066f, (byte) 12 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 281458, 473.17914f, 526.8003f, 563.77765f, (byte) 4 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 281458, 471.37543f, 528.8726f, 564.4679f, (byte) 5 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 281458, 469.89398f, 531.47705f, 565.9161f, (byte) 10 , 3600);
				spawn(PVPInstanceService.getPVPInstanceMap().getId(), PVPInstanceService.getPVPInstanceId(), 281458, 468.77225f, 533.7574f, 567.6507f, (byte) 39 , 3600);
				break;
		}
	}
       
       private static void runwalk(final Npc npc, String wlk){
   		npc.getSpawn().setWalkerId(wlk);
   		WalkManager.startWalking((NpcAI2) npc.getAi2());
   		PacketSendUtility.broadcastPacket(npc, new SM_EMOTION(npc, EmotionType.START_EMOTE2, 0, npc.getObjectId()));
   		((AbstractAI) npc.getAi2()).setStateIfNot(AIState.WALKING);
   	}
         
       
}
